package com.stylefeng.guns.modular.profitLoss.controller;


import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.stylefeng.guns.core.aop.Platform;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.profitLoss.model.ProfitLoss;
import com.stylefeng.guns.modular.profitLoss.service.IProfitLossService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 损益报表控制器
 * @time 2018-07-24
 * @auther laoyu
 */
@Controller
@RequestMapping("/profitLoss")
public class ProfitLossController {

    private String PREFIX = "/profitLoss/profitLoss/";


    @Autowired
    private IProfitLossService profitLossService;

    @RequestMapping("")
    public String index() {
        return PREFIX + "profitLoss.html";
    }


    /**
     * 获取平台损益报表
     * @param revDate
     * @param platform
     * @return
     * @time 2018-07-24
     * @auther laoyu
     */
    @RequestMapping("/getProfitLoss")
    @ResponseBody
    public Object getProfitLoss(@Platform String platform, String revDate)throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date rev_date = null;
        if (StringUtils.isNotBlank(revDate)){
            rev_date=simpleDateFormat.parse(revDate);
        }else {
            rev_date = new Date();
        }
            return profitLossService.getProfitLoss(platform,rev_date);


    }

    /**
     * 描述:导出损益表
     * 创建人: laoyu
     * 日期: 2018/8/1 0001
     * 时间: 下午 4:16
     */
    @RequestMapping("/load")
    @ResponseBody
    public void downloadByPoiBaseView(ModelMap modelMap, HttpServletRequest request,
                                      HttpServletResponse response, ProfitLoss profitLoss) throws Exception {



        //查询要导出工资
        if (StringUtils.isBlank(profitLoss.getRevDate())){
            throw new GunsException(BizExceptionEnum.BUSINSS_ERROR,"请选择要导出的损益表时间");
        }

        String platformName=ShiroKit.getUser().getDeptName();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");

        List<ProfitLoss>  profitLossList = profitLossService.getProfitLoss(ShiroKit.getUser().getDeptcode(),simpleDateFormat.parse(profitLoss.getRevDate()));;

        TemplateExportParams params = new TemplateExportParams(
                "exceldoc/profitLoss.xlsx");

        modelMap.put(TemplateExcelConstants.FILE_NAME, platformName+"损益表");
        modelMap.put(TemplateExcelConstants.PARAMS, params);
        modelMap.put(TemplateExcelConstants.MAP_DATA, profitLossService.load(profitLossList,profitLoss.getRevDate(),profitLoss.getPlatform(), platformName));
        PoiBaseView.render(modelMap, request, response,
                TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);

    }



}
